<?php

/**
 * MVM_MALL 网上商店系统 商品分类管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-02-03 $
 * $Id: category.inc.php  www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if ($action=='list'){
    ///读入分类缓存
    $arr_tree = $cache->get_cache('tree');
    require_once template('category');
    footer();
} elseif($action=='add'){
    if($setp==1&&$_POST){
        //图片上传
        if ($_FILES['category_file1']['name']!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            $f = new upfile('gif,jpg,png,bmp',"./images/category/");
            $category_file1_text = $f->upload('category_file1');
        }
        $use_direct    = (int)$use_direct;
        $category_id   = (int)$category_id;
        $cat_in        = false;
        $query = " INSERT INTO `{$tablepre}category` SET
                      category_id         = '$category_id',
                      category_name       = '$category_name',
                      category_key        = '$category_key',
                      category_desc       = '$category_desc',
                      category_skin       = '$category_skin',
                      category_rank       = '$category_rank',
                      category_grant      = '$category_grant',
					  category_file1      = '$category_file1_text',
                      use_html            = '$use_html',
                      category_head       = '$category_head',
                      category_foot       = '$category_foot',
					  category_admin_memo = '$category_admin_memo',
					  use_direct          = '$use_direct',
                      register_date       = '$m_now_time'";
        $db->query($query);
        //更新缓存在读取
        $cache->put_cache('tree');
        admin_msg('success','admincp.php?module=category&action=list');
    } else {
        //摸板选择
        $tpl_arr      = tpl_array("./templates/$mm_skin_name",'category');
        $tpl_menu     = drop_menu($tpl_arr,'category_skin',$category_skin);
        $grant_menu   = drop_menu($cache->get_cache('grade'),'category_grant');
        $cat_menu     = cat_menu('category_id',$category_id);
        $use_direct_y = 'checked';
        require_once template('category_add');
       footer();
   }
} elseif($action=='edit' && is_numeric($uid)){
    $cat_rt = $db->get_one("SELECT * FROM `{$tablepre}category` WHERE uid='$uid'");
    if($setp==1&&$_POST){
        //图片上传
        if ($_FILES['category_file1']['name']!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            $f = new upfile('gif,jpg,png,bmp',"./images/category/");
            $category_file1_text = $f->upload('category_file1');
        }else {
            $category_file1_text = $cat_rt['category_file1'];
        }
        $use_direct    = (int)$use_direct;
        $category_id   = (int)$category_id;
        $query = " UPDATE `{$tablepre}category` SET
                      category_id         = '$category_id',
                      category_name       = '$category_name',
                      category_key        = '$category_key',
                      category_desc       = '$category_desc',
                      category_skin       = '$category_skin',
                      category_rank       = '$category_rank',
                      category_grant      = '$category_grant',
					  category_file1      = '$category_file1_text',
                      use_html            = '$use_html',
                      category_head       = '$category_head',
                      category_foot       = '$category_foot',
					  category_admin_memo = '$category_admin_memo',
					  use_direct          = '$use_direct',
                      register_date       = '$m_now_time'
                      WHERE uid = '$uid'";
        $db->query($query);
        $cache->put_cache('tree');
        admin_msg('success','admincp.php?module=category&action=list');
    } else {
        @extract($cat_rt,EXTR_OVERWRITE);
        //摸板选择
        $tpl_arr      = tpl_array("./templates/$mm_skin_name",'category');
        $tpl_menu     = drop_menu($tpl_arr,'category_skin',$category_skin);
        $grant_menu   = drop_menu($cache->get_cache('grade'),'category_grant');
        $cat_menu     = cat_menu('category_id',$category_id);
        drop_check($use_direct,'use_direct');
        drop_check($use_html,'use_html');
        $cat_img = $category_file1 ? "<a href='javascript:;' onclick=\"remove('$uid','admincp.php?module=category&action=del&type=delimg&uid=$uid');\"><img src=\"images/admincp/delet.gif\" border=\"0\"></a>":'';
        require_once template('category_add');
       footer();
   }
} elseif ($action=='del') {
    //删除分类图片
    if ($type=='delimg' && is_numeric($uid)) {
        $rs = $db->get_one("SELECT category_file1,uid FROM `{$tablepre}category` WHERE uid='$uid'");
        if($rs['category_file1']) {
            file_unlink($rs['category_file1']);
            $db->query("UPDATE `{$tablepre}category` SET category_file1='' WHERE uid='$uid'");
        }
        $cache->put_cache('tree');
        exit;
    }else {
        //删除分类
        $cat_rt = $db->get_one("SELECT * FROM `{$tablepre}category` WHERE category_id='$uid'");
        $cat_rt && admin_msg('admin_class_existence');
        //删除商品
        $result = $db->query("SELECT uid,goods_category,goods_file1,goods_file2 FROM `{$tablepre}goods_table` WHERE goods_category = '$uid'");
        while($rt = $db->fetch_array($result)) {
            if($rt['uid']) {
                $rt['goods_file1'] && file_unlink($rt['goods_file1']);
                $rt['goods_file2'] && file_unlink($rt['goods_file2']);
                $db->query("DELETE FROM `{$tablepre}goods_table` WHERE uid='$rt[uid]'");
                $db->query("DELETE FROM `{$tablepre}gallery` WHERE goods_id='$rt[uid]'");
            }

        }
        $db->query(" DELETE FROM `{$tablepre}category`  WHERE uid = '$uid'");//删除分类
    }
    $cache->put_cache('tree');
    admin_msg('success',dreferer());
}
else {
    admin_msg('pass_worng');
}
?>
